package com.chenfan.ccp.entity;

import java.io.Serializable;
import java.util.HashMap;

/**
 * 统一处理response
 */
public class Response implements Serializable {

    protected static final int SUCCESS_CODE = 200;
    protected static final String EMPTY = "";
    protected static final int ZERO = 0;
    protected static final int ERROR_CODE = 400;
    protected static final int FAIL_CODE = 401;
    protected static final int CRASH_CODE = 500;
    protected static final int RETRY_CODE = 504;
    private static final ThreadLocal<Response> TOTAL_RESPONSE = new InheritableThreadLocal<>();
    protected Integer code;
    protected String msg;
    protected Object data;
    protected Number count;

    protected Response() {

    }


    public static Response response() {
        Response response = TOTAL_RESPONSE.get();
        if (response != null) {
            TOTAL_RESPONSE.remove();
        }
        return response;
    }

    public static Response success() {
        return pack(SUCCESS_CODE, EMPTY, new HashMap<>(), ZERO);
    }

    public static Response success(Object data) {
        return pack(SUCCESS_CODE, EMPTY, data, ZERO);
    }

    public static Response success(Object data, Number count) {
        return pack(SUCCESS_CODE, EMPTY, data, count == null ? ZERO : count);
    }

    public static Response success(String msg) {
        return pack(SUCCESS_CODE, msg, new HashMap<>(), ZERO);
    }

    public static Response success(Object data, String msg) {
        return pack(SUCCESS_CODE, msg, data, ZERO);
    }

    public static Response error() {
        return pack(ERROR_CODE, EMPTY, new HashMap<>(), ZERO);
    }

    public static Response error(Object data) {
        return pack(ERROR_CODE, EMPTY, data, ZERO);
    }

    public static Response error(Object data, Number count) {
        return pack(ERROR_CODE, EMPTY, data, count == null ? ZERO : count);
    }

    public static Response error(String msg) {
        return pack(ERROR_CODE, msg, new HashMap<>(), ZERO);
    }

    public static Response error(Object data, String msg) {
        return pack(ERROR_CODE, msg, data, ZERO);
    }

    public static Response fail() {
        return pack(FAIL_CODE, EMPTY, new HashMap<>(), ZERO);
    }

    public static Response crash() {
        return pack(CRASH_CODE, EMPTY, new HashMap<>(), ZERO);
    }

    public static Response retry() {
        return pack(RETRY_CODE, EMPTY, new HashMap<>(), ZERO);
    }

    public static Response response(int code, String msg) {
        return pack(code, msg, new HashMap<>(), ZERO);
    }

    public static Response response(int code, Object data, String msg) {
        return pack(code, msg, data, ZERO);
    }

    public static Response response(int code, String msg, Number count) {
        return pack(code, msg, new HashMap<>(), count == null ? ZERO : count);
    }

    public static Response response(int code, Object data, Number count) {
        return pack(code, EMPTY, data, count == null ? ZERO : count);
    }

    public static Response response(int code, String msg, Object data, Number count) {
        return pack(code, msg, data, count == null ? ZERO : count);
    }

    public static Response response(Response response) {
        return response;
    }

    protected static Response pack(int code, String msg, Object data, Number count) {
        Response response = new Response();
        response.setCode(code);
        response.setMsg(msg);
        response.setData(data);
        response.setCount(count);

        return response;
    }

    public void build() {
        TOTAL_RESPONSE.set(this);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Number getCount() {
        return count;
    }

    public void setCount(Number count) {
        this.count = count;
    }
}
